﻿using MongoDB.Bson;
using System;
using System.Collections.Generic;
namespace hlog {
    public class ObjectUtil2 {
       
       
        public static BsonDocument def0(BsonDocument nowDoc) {
            BsonDocument bd  = new BsonDocument(); ;
            IEnumerable<BsonElement> elements = nowDoc.Elements;
            foreach (BsonElement be in elements) {
                String name = be.Name;
                 if( name == "path" ) {
                    continue;
                }
                BsonValue nowv = be.Value;
                var defValue = def2(nowv);
                if( defValue != null) {
                    bd.Add(name ,defValue);
                }
               
            }
            return bd;
        }
       
        static BsonValue def2(BsonValue nowv) {
            if( nowv.IsBsonArray ) {
                BsonArray arrayNew = new BsonArray();
                BsonArray array = nowv.AsBsonArray;
               
                    for( int i = 0;i < array.Count;i++ ) {
                        BsonDocument bdc = def0(array[i].AsBsonDocument);
                        if( bdc.ElementCount != 0 ) {
                            arrayNew.Add(bdc);
                        }
                    }
                    if( arrayNew.Count != 0 ) {
                        return arrayNew;
                    }
                
            } else if( nowv.IsBsonDocument ) {
                
                BsonDocument defValue = def0(nowv.AsBsonDocument);
                if( defValue.ElementCount != 0 ) {
                    return defValue;
                }
              
            } else {
                if("0" != nowv.ToString() ) {
                   return nowv;
                }
            }
             return null;
         }
     }
}
